#include "openmove_Prefix.h"
#include "RGBSlider.h"
#include <CEGUI/elements/CEGUISlider.h>
#include <CEGUI/elements/CEGUIStaticText.h>
#include <CEGUI/elements/CEGUIPushButton.h>

RGBASlider :: RGBASlider(void)
{
}

RGBASlider :: RGBASlider(String const &name, CEGUI::Window *parent, Vector4 const vector, CEGUI::Point const position, CEGUI::Size const size) : m_vector(vector)
{
	create(name, parent, vector, position, size);
}

void RGBASlider :: create(String const &name, CEGUI::Window *parent, Vector4 const vector, CEGUI::Point const position, CEGUI::Size const size)
{
	CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();

	m_window = wmgr.createWindow((CEGUI::utf8*)"DefaultWindow", (CEGUI::utf8*)name.c_str());

	m_window->setPosition(position);
	m_window->setSize(size);

	m_r = (CEGUI::Slider*)wmgr.createWindow("TaharezLook/Slider", (CEGUI::utf8*)String(name + "R").c_str());
	m_window->addChildWindow(m_r);
	m_r->setPosition(CEGUI::Point(0.f, 0.f));
	m_r->setSize(CEGUI::Size(0.33f, 0.60f));
	m_r->setMaxValue(1.0f);
	
	m_g = (CEGUI::Slider*)wmgr.createWindow("TaharezLook/Slider", (CEGUI::utf8*)String(name + "G").c_str());
	m_window->addChildWindow(m_g);
	m_g->setPosition(CEGUI::Point(0.33f, 0.0f));
	m_g->setSize(CEGUI::Size(0.33f, 0.60f));
	m_g->setMaxValue(1.0f);

	m_b = (CEGUI::Slider*)wmgr.createWindow("TaharezLook/Slider", (CEGUI::utf8*)String(name + "B").c_str());
	m_window->addChildWindow(m_b);
	m_b->setPosition(CEGUI::Point(0.66f, 0.0f));
	m_b->setSize(CEGUI::Size(0.33f, 0.60f));
	m_b->setMaxValue(1.0f);

	CEGUI::StaticText *diffuseText = (CEGUI::StaticText*)wmgr.createWindow("TaharezLook/StaticText", (CEGUI::utf8*)String(name + "Value").c_str());
	m_window->addChildWindow(diffuseText);
	diffuseText->setPosition(CEGUI::Point(0.f, 0.60f));
	diffuseText->setSize(CEGUI::Size(1.f, 0.4f));
	m_value = diffuseText;

	m_r->subscribeEvent(CEGUI::Slider::EventValueChanged, CEGUI::Event::Subscriber(&RGBASlider::handleSliderEvent, this));
	m_g->subscribeEvent(CEGUI::Slider::EventValueChanged, CEGUI::Event::Subscriber(&RGBASlider::handleSliderEvent, this));
	m_b->subscribeEvent(CEGUI::Slider::EventValueChanged, CEGUI::Event::Subscriber(&RGBASlider::handleSliderEvent, this));

	parent->addChildWindow(m_window);
}

RGBASlider :: ~RGBASlider(void)
{
	CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
	wmgr.destroyWindow(m_window);
}

void RGBASlider :: updateSlider(ColourValue const value)
{
	updateSlider(Vector4(value.r, value.g, value.b, value.a));
}

void RGBASlider :: updateSlider(Vector3 const value)
{
	updateSlider(Vector4(value.x, value.y, value.z, 1));
}

void RGBASlider :: updateSlider(Vector4 const value)
{
	Real const tmp = (m_max - m_min);
	m_vector.x = (value.x - m_min) / tmp;
	m_vector.y = (value.y - m_min) / tmp;
	m_vector.z = (value.z - m_min) / tmp;
	m_vector.w = (value.w - m_min) / tmp;
	
	m_r->setCurrentValue((value.x - m_min) / tmp);
	m_g->setCurrentValue((value.y - m_min) / tmp);
	m_b->setCurrentValue((value.z - m_min) / tmp);
	String val = StringConverter::toString(value.x) + " " + StringConverter::toString(value.y) + " " + StringConverter::toString(value.z) + " " + StringConverter::toString(value.w);
	m_value->setText(val);
}

bool RGBASlider :: isActive(void)
{
	return (m_r->isActive() || m_g->isActive() || m_b->isActive());
}

bool RGBASlider :: handleSliderEvent(const CEGUI::EventArgs& e)
{
	m_vector.x = m_r->getCurrentValue();
	m_vector.y = m_g->getCurrentValue();
	m_vector.z = m_b->getCurrentValue();
	String val = StringConverter::toString(getValue().x) + " " + StringConverter::toString(getValue().y) + " " + StringConverter::toString(getValue().z) + " " + StringConverter::toString(getValue().w);
	m_value->setText(val);

	return (true);
}

void RGBASlider :: setVisible(bool const visible)
{
	m_window->setVisible(visible);
}

bool RGBASlider :: isVisible(void)
{
	return (m_window->isVisible());
}

void RGBASlider :: setMin(Real const val)
{
	m_min = val;
}

void RGBASlider :: setMax(Real const val)
{
	m_max = val;
}

void RGBASlider :: setMinMax(Real const val)
{
	setMin(val);
	setMax(val);
}

Vector4 RGBASlider :: getValue(void)
{
	Real const tmp = (m_max - m_min);
	return (Vector4(m_vector.x * tmp + m_min, m_vector.y * tmp + m_min, m_vector.z * tmp + m_min, m_vector.w * tmp + m_min));
}

Vector3 RGBASlider :: getValueVector3(void)
{
	Vector4 const value = getValue();
	return (Vector3(value.x, value.y, value.z));
}

ColourValue RGBASlider :: getValueColour(void)
{
	Vector4 const value = getValue();
	return (ColourValue(value.x, value.y, value.z, value.w));
}
